﻿/**
 * jQuery EasyUI 1.4
 * 
 * Copyright (c) 2009-2014 www.jeasyui.com. All rights reserved.
 * 
 * Licensed under the GPL license: http://www.gnu.org/licenses/gpl.txt To use it
 * on other terms please contact us at info@jeasyui.com
 * 
 */
(function($) {
	function createButcombogrid(target) {
		var butcombogrid = $.data(target, "butcombogrid");
		var opts = butcombogrid.options;
		var grid = butcombogrid.grid;
		$(target).addClass("butcombogrid-f").butcombo($.extend({},opts,{
			onShowPanel : function() {
				var p = $(this).butcombo("panel");
				var outerHeight = p.outerHeight()- p.height();
				var minHeight = p._size("minHeight");
				var maxHeight = p._size("maxHeight");
				var resize={
					width : "100%",
					height : (isNaN(parseInt(opts.panelHeight)) ? "auto" : "100%"),
					minHeight : (minHeight ? minHeight - outerHeight : ""),
					maxHeight : (maxHeight ? maxHeight - outerHeight : "")
				};
				$(this).butcombogrid("grid").datagrid("resize",resize);
			opts.onShowPanel.call(this);
		}}));
		var panel = $(target).butcombo("panel");
		if (!grid) {
			grid = $("<table></table>").appendTo(panel);
			butcombogrid.grid = grid;
		}
		var outerHeight = panel.outerHeight()- panel.height();
		var minHeight = panel._size("minHeight");
		var maxHeight = panel._size("maxHeight");
		grid.datagrid($.extend({}, opts, {
			width : "100%",
			height : (isNaN(parseInt(opts.panelHeight)) ? "auto" : "100%"),
			minHeight : (minHeight ? minHeight - outerHeight : ""),
			maxHeight : (maxHeight ? maxHeight - outerHeight : ""),
			showPageList:false,
			showPageStyle:"manual",
			border : false,
			pagination:true,
			singleSelect : (!opts.multiple)
		}));
	};
	
	$.fn.butcombogrid = function(options, param) {
		if (typeof options == "string") {
			var method = $.fn.butcombogrid.methods[options];
			if (method) {
				return method(this, param);
			} else {
				return this.linkbutton(options, param);
			}
		}
		options = options || {};
		return this.each(function() {
			var butcombogrid = $.data(this, "butcombogrid");
			if (butcombogrid) {
				$.extend(butcombogrid.options, options);
				if (options.value != undefined) {
					butcombogrid.options.originalValue = options.value;
				}
			} else {
				butcombogrid = $.data(this, "butcombogrid", {
					options : $.extend({}, $.fn.butcombogrid.defaults, $.fn.butcombogrid.parseOptions(this), options),
					previousText : ""
				});
				butcombogrid.options.originalValue = butcombogrid.options.value;
			}
			createButcombogrid(this);
		});
	};
	$.fn.butcombogrid.methods = {
		options : function(jq) {
			return $.data(jq[0], "butcombogrid").options;
		},
		grid : function(jq) {
			return $.data(jq[0], "butcombogrid").grid;
		}
	};
	$.fn.butcombogrid.parseOptions = function(target) {
		return $.extend({}, $.fn.butcombo.parseOptions(target), $.parser.parseOptions(target, ["idField","textField", "mode"]));
	};
	$.fn.butcombogrid.defaults = $.extend({}, $.fn.butcombo.defaults, {
		
	});
})(jQuery);
